package com.example.administrator.xxnapp.fragment.event;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.EventBean;
import com.example.administrator.xxnapp.bean.UserBean;
import com.example.administrator.xxnapp.event.EditEventActivity;
import com.example.administrator.xxnapp.util.Config;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mdruby.baselibrary.BaseFragment;
import cn.mdruby.baselibrary.utils.ObjectStoreUtil;

/**
 */
public class EventFragment extends BaseFragment implements IViewEvent,OnRefreshListener{
    private static EventFragment fragment;
    public static EventFragment getInstance(){
        if (fragment == null){
            synchronized (EventFragment.class){
                if (fragment == null){
                    fragment = new EventFragment();
                }
            }
        }
        return fragment;
    }
    private EventPresenter mPresenter;
    private List<EventBean> mDatas;
    private RVEventAdapter mAdapter;
    private UserBean user;

    @BindView(R.id.frag_event_RV)
    RecyclerView mRV;
    @BindView(R.id.frag_event_IV_edit)
    ImageView mIVEdit;
    @BindView(R.id.frag_event_SmartRefreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_event;
    }

    @Override
    public void initBeforeDatas() {
        mDatas = new ArrayList<>();
        mPresenter = new EventPresenter(this,new EventBean());
        user = (UserBean) ObjectStoreUtil.getObject(mContext, Config.Key.USER_OBJECT_KEY);
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this,view);
    }

    @Override
    public void setListeners() {
        mRV.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new RVEventAdapter(mContext,mDatas);
        mRV.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableLoadmore(false);

        mPresenter.getEvent(user.getUserId());
    }

    @Override
    public void getEeventSuccess(List<EventBean> beans) {
        mDatas.clear();
        mDatas.addAll(beans);
        mAdapter.notifyDataSetChanged();
        mRefreshLayout.finishRefresh(200);
    }

    @Override
    public void getEeventError(int resultCode, String message) {
        mRefreshLayout.finishRefresh(false);
    }

    @Override
    public void saveEventSuccess(String message) {

    }

    @Override
    public void saveEventError(int resultCode, String message) {

    }

    @OnClick(R.id.frag_event_IV_edit)
    public void editEvent(View view){
        Intent intent = new Intent(mContext, EditEventActivity.class);
        startActivityForResult(intent,Config.RequestCode.EDIT_EVENT);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK){
            switch (requestCode){
                case Config.RequestCode.EDIT_EVENT:
                    mPresenter.getEvent(user.getUserId());
                    break;
            }
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getEvent(user.getUserId());
    }
}
